package com.huoke.web.controller.system;

import cn.hutool.core.util.IdUtil;
import com.huoke.common.annotation.Log;
import com.huoke.common.annotation.RepeatSubmit;
import com.huoke.common.core.controller.BaseController;
import com.huoke.common.core.domain.AjaxResult;
import com.huoke.common.core.domain.entity.SysDictData;
import com.huoke.common.core.domain.entity.SysUser;
import com.huoke.common.enums.BusinessType;
import com.huoke.common.utils.DateUtils;
import com.huoke.common.utils.StringUtils;
import com.huoke.system.domain.SysConfig;
import com.huoke.system.domain.SysNotice;
import com.huoke.system.domain.TXx;
import com.huoke.system.service.*;
import lombok.extern.log4j.Log4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zhaoyongke
 * @create: 2025-09-05 17:33
 * @description: aa
 */
@RestController
@RequestMapping("/api")
public class ApiController extends BaseController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ITXxService tXxService;

    @Autowired
    private ISysUserService userService;


    @GetMapping("/nologin/getConfig")
    public AjaxResult getConfig( )
    {
        SysNotice sysConfig = new SysNotice();
        List<SysNotice> list = noticeService.selectNoticeList(sysConfig);
        if(list.size()>0)
        {
            return success(list.get(0));
        }
        return AjaxResult.success(sysConfig);
    }


    @Autowired
    private ISysDictTypeService dictTypeService;


    @GetMapping(value = "/nologin/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return success(data);
    }

    @Autowired
    TencentSmsTemplate  tencentSmsTemplate;


    @PostMapping("/nologin/addXx")
    @RepeatSubmit
    public AjaxResult addXx(@Validated  @RequestBody TXx tXx)
    {
        tXx.setId(IdUtil.simpleUUID());
        tXx.setCreateTime(DateUtils.getNowDate());
        tXx.setStatus("1");
        tXxService.insertTXx(tXx);
        try {
            String userId = tXx.getUserId();
            String parentUserId = tXx.getParentUserId();
            SysUser sysUser = userService.selectUserById(Long.valueOf(userId));
            // 发送验证码
            String phonenumber = sysUser.getPhonenumber();
            String parentPhonenumber="";
            if(!StringUtils.isEmpty(parentUserId)){
                SysUser parentsysUser = userService.selectUserById(Long.valueOf(parentUserId));
                 parentPhonenumber = parentsysUser.getPhonenumber();
            }
            SysUser adminuser= userService.selectUserByUserName("admin");
            String adminPhonenumber = adminuser.getPhonenumber();
            if(StringUtils.isNotEmpty(parentPhonenumber)){
                String[] phoneNumbers = new String[]{phonenumber,adminPhonenumber,parentPhonenumber};
                String s = tencentSmsTemplate.sendSms(phoneNumbers, null,null);
                logger.info("发送结果:"+s);
            }else {
                String[] phoneNumbers = new String[]{phonenumber,adminPhonenumber};
                String s = tencentSmsTemplate.sendSms(phoneNumbers, null,null);
                logger.info("发送结果:"+s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.success();
    }


    @PostMapping("/nologin/sendPhone")
    @RepeatSubmit
    public AjaxResult sendPhone()
    {
        String[] phoneNumbers = new String[]{"15054115351"};
        // 发送验证码
        String s = tencentSmsTemplate.sendSms(phoneNumbers, null,null);
        logger.info("发送结果:"+s);
        return AjaxResult.success();
    }




}
